<template>
  <div :class="className" :style="{ height: height, width: width }" />
</template>

<script>
// import echarts from "echarts";
// require("echarts/theme/macarons"); // echarts theme
import resize from "./mixins/resize";

export default {
  mixins: [resize],
  props: {
    className: {
      type: String,
      default: "chart",
    },
    width: {
      type: String,
      default: "100%",
    },
    height: {
      type: String,
      default: "300px",
    },
    autoResize: {
      type: Boolean,
      default: true,
    },
    chartData: {
      type: Object,
      required: true,
    },
    titleText: {
      type: String,
      default: "审批耗时",
    },
    timeValue: {
      type: Number,
      default: 16.2,
    },
  },
  data() {
    return {
      chart: null,
    };
  },
  watch: {
    chartData: {
      deep: true,
      handler(val) {
        this.setOptions(val);
      },
    },
  },
  mounted() {
    this.$nextTick(() => {
      this.initChart();
    });
  },
  beforeDestroy() {
    if (!this.chart) {
      return;
    }
    this.chart.dispose();
    this.chart = null;
  },
  methods: {
    initChart() {
      this.chart = this.$echarts.init(this.$el, "macarons");
      this.setOptions(this.chartData);
    },
    setOptions({ oneData, twoData, xdata } = {}) {
      this.chart.setOption({
        xAxis: {
          data: xdata,
          boundaryGap: false,
          axisTick: {
            show: false,
          },
          axisLabel: {
            textStyle: {
              fontWeight: 400,
              color: "#999999",
            },
          },
          axisLine: {
            show: false, // X轴 网格线 颜色类型的修改
          },
        },
        tooltip: {
          trigger: "axis",
          axisPointer: {
            type: "cross",
          },
          padding: [5, 10],
        },
        yAxis: {
          axisTick: {
            show: false,
          },
          axisLabel: {
            textStyle: {
              fontWeight: 400,
              color: "#999999",
            },
          },
          axisLine: {
            show: false, // X轴 网格线 颜色类型的修改
          },
        },
        legend: {
          bottom: "2%",
        },
        title: [
          {
            text: "{a|}",
            left: "5%",
            textStyle: {
              rich: {
                a: {
                  height: 16,
                  width: 4,
                  lineHight: 16,
                  backgroundColor: "#567BCB",
                },
              },
            },
          },
          {
            text: this.titleText,
            left: "7%",
            textStyle: {
              fontSize: 16,
              width: 100,
              color: "#454c5c",
              fontWeight: 500,
              lineHeight: 20,
            },
          },
          {
            text: "平均用时 {a|" + this.timeValue + "h}",
            right: 0,
            textStyle: {
              fontSize: 16,
              fontWeight: 500,
              color: "#454c5c",
              rich: {
                a: {
                  padding: [0, 10],
                  fontSize: 20,
                  fontWeight: "bolder",
                },
              },
            },
          },
        ],
        series: [
          {
            name: oneData.name,
            itemStyle: {
              normal: {
                color: "#4A79EB",
                lineStyle: {
                  color: "#4A79EB",
                  width: 2,
                },
              },
            },
            type: "line",
            data: oneData.expectedData,
          },
          {
            name: twoData.name,
            smooth: true,
            stack: "Total",
            type: "line",
            itemStyle: {
              normal: {
                color: "#8AEDCB",
                lineStyle: {
                  color: "#8AEDCB",
                  width: 2,
                },
              },
            },
            data: twoData.actualData,
          },
        ],
      });
    },
  },
};
</script>
